﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ITJZ.BitHelper.IAPI;
using System.Data.OleDb;

namespace ITJZ.BitHelper.Web.API
{
    public  class User:IUser
    {
        #region IUser 成员

        public APIModel.User Login(string email, string password)
        {
            ITJZ.BitHelper.APIModel.User user = new ITJZ.BitHelper.APIModel.User();
            string sql= @"SELECT * FROM [User] WHERE (email = @email) AND ([password] = @password)";

            using (OleDbConnection conn = new OleDbConnection(Config.connStr))
            {
                using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@email", email);
                    cmd.Parameters.AddWithValue("@password", password);


                    conn.Open();
                    OleDbDataReader reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        user.Uid = (string)reader["uid"];
                        user.Email = email;
                        user.LastLoginIp = HttpContext.Current.Request.UserHostAddress;
                        user.StateKey = HttpContext.Current.Session.SessionID;
                        user.LastLoginTime = DateTime.Now;

                        HttpContext.Current.Application.Lock();

                        HttpContext.Current.Application[user.StateKey] = user;

                        HttpContext.Current.Application.UnLock();
                    }
                    else
                    {
                        throw new Exception("登录失败");
                    }
                }
            }

            return user;
        }

        public ITJZ.BitHelper.APIModel.ResInfo Register(string email, string password)
        {
            ITJZ.BitHelper.APIModel.ResInfo res = new ITJZ.BitHelper.APIModel.ResInfo();

            using (OleDbConnection conn = new OleDbConnection(Config.connStr))
            {

                string sql = @"
INSERT INTO [User]
      (name, email, [password], regTime)
VALUES (@name,@email,@password,@regTime)
";
                using (OleDbCommand cmd = new OleDbCommand(sql,conn))
                {
                    cmd.Parameters["@name"].Value = email;
                    cmd.Parameters["@email"].Value = email;
                    cmd.Parameters["@password"].Value = password;
                    cmd.Parameters["@regTime"].Value = DateTime.Now;


                    conn.Open();
                    int ret = cmd.ExecuteNonQuery();

                    if (ret>0)
                    {
                        res.ErrState = 0;
                        res.Msg = "注册成功";
                    }
                    else
                    {

                        res.ErrState = 1;
                        res.Msg = "注册失败，原因未知";
                    }
                }
            }

            return res;
        }

        #endregion

        /// <summary>
        /// 根据key判断是否已经登录
        /// </summary>
        /// <param name="key">用户通过用户名密码登录后返回的登录密钥</param>
        /// <returns></returns>
        public APIModel.User LoginByKey(string key)
        {
            object oUser = HttpContext.Current.Application[key];

            if (oUser==null)
            {
                throw new Exception("您尚未登录");
            }

            APIModel.User user = oUser as APIModel.User;

            return user;
        }


        internal static ITJZ.BitHelper.APIModel.User GetUser()
        {

            string key = HttpContext.Current.Request["key"] as string;

            if (key==null)
            {
                throw new Exception("缺少参数key");
            }

            object oUser = HttpContext.Current.Application[key];

            if (oUser == null)
            {
                throw new Exception("您尚未登录");
            }

            APIModel.User user = oUser as APIModel.User;

            return user; 
        }

  
    }
}
